<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">商品</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">品牌管理</a></el-breadcrumb-item>
    </el-breadcrumb>

  <el-card>
    <!-- 分页搜索表单 -->
    <el-form :inline="true" :model="searchMap" class="demo-form-inline" size="small">
      <el-form-item label="编号">
        <el-input v-model="searchMap.id" type="number" placeholder="输入品牌编号"></el-input>
      </el-form-item>
      <el-form-item label="品牌名称">
        <el-input v-model="searchMap.name" @input="e => searchMap.name = validForbid (e)" placeholder="输入品牌名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getBrandList()"  icon="el-icon-search" circle></el-button>&nbsp;&nbsp;&nbsp;
        <el-button size='small' type="primary" @click="dialogVisible = true" icon = "el-icon-circle-plus-outline"></el-button>
      </el-form-item>
    </el-form>
    <!-- 分页搜索所显示的数据 -->
      <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="编号"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="品牌名称"
        width="180">
      </el-table-column>
      <el-table-column
        label="品牌图片">
        <template slot-scope="scope">
          <img :src="scope.row.image" style="height:50px;"/>
        </template>
      </el-table-column>
      <el-table-column
        prop="letter"
        label="品牌首字母">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button size='mini' @click="showdata(scope.row)" type="warning" icon="el-icon-edit" circle></el-button>
          <el-button size='mini' @click="remove(scope.row.id)" type="danger" icon="el-icon-delete" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页插件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNo"
      :page-sizes="[8, 16, 32, 64]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
    <!-- 新增/修改的弹出框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
        <el-form ref="form" :model="showBrand" label-width="80px">
            <el-form-item>
              <el-input v-model="showBrand.id" type="hidden"></el-input>
            </el-form-item>
            <el-form-item label="品牌名称">
              <el-input v-model="showBrand.name"  @input="e => showBrand.name = validForbid (e)"></el-input>
            </el-form-item>
            <el-form-item label="品牌图片">
                <el-upload
                  class="avatar-uploader"
                  action="http://localhost:88/api/goods/youthOrange/uploadOss/upload?host=imgsBrand"
                  list-type="picture-card"
                  :on-success="successAliyun"
                  :before-upload="beforeAvatarUpload"
                  >
                  <!-- <i class="el-icon-plus"></i> -->
                <!--<span v-if="showBrand.image">当前image:&nbsp;</span> -->
                 <img v-if="showBrand.image" :src="showBrand.image" class="avatar">
                </el-upload>
                <!-- <el-dialog :visible.sync="ossdialogVisible">
                  <img width="30%" :src="showBrand.image" alt="">
                </el-dialog> -->
            </el-form-item>
            <el-form-item label="品牌字母">
              <el-input v-model="showBrand.letter"  @input="e => showBrand.letter = validForbid (e)"></el-input>
            </el-form-item>
        </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clearSouceData()">取 消</el-button>
        <el-button type="primary" @click="isAlterSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
const defaultBrandData = {
  id: 0,
  name: '',
  image: '',
  letter: '',
  seq: ''
}
export default {
  data () {
    return {
      tableData: [], // 表格数据
      pageNo: 1, // 页号
      pageSize: 8, // 页大小
      searchMap: {}, // 查询数据
      total: 0, // 总页数
      dialogVisible: false, // 新增修改的显示框
      showBrand: defaultBrandData, // 新增修改提交/回显的val
      ossdialogVisible: false,
      title: 'add'
    }
  },
  created () {
    this.getBrandList()
  },
  methods: {
    // 分页
    async getBrandList () {
      const { data: res } = await this.$http.post(`/goods/brand/pageQueryByAll/${this.pageNo}/${this.pageSize}`, this.searchMap)
      // console.log(res)
      this.tableData = res.data.pageUtils.rows
      this.total = res.data.pageUtils.total
    },
    handleSizeChange (newSize) {
      this.pageSize = newSize
      this.getBrandList()
    },
    handleCurrentChange (newPageNo) {
      this.pageNo = newPageNo
      this.getBrandList()
    },
    // 删除
    async remove (id) {
      const { data: res } = await this.$http.delete(`/goods/brand/delete?id=${id}`)
      if (res.code === 200) {
        alert('删除成功')
        this.getBrandList()
      }
    },
    // 修改新增之前的判断
    isAlterSubmit () {
      console.log(!this.showBrand.id)
      // 通过点击事件传参，id判断做那个操作
      if (!this.showBrand.id) {
        this.addBrand()
      } else {
        this.updateBrand()
      }
      this.clearSouceData()
    },
    async addBrand () {
      await this.$http.post('/goods/brand/addBrand', this.showBrand)
    },
    // 修改回显
    async showdata (row) {
      this.title = 'update'
      this.dialogVisible = true
      const { data: res } = await this.$http.get(`/goods/brand/detail/${row.id}`)
      // console.log(res)
      this.showBrand = res.data.tbBrand
      this.showdata.id = res.data.tbBrand.id // 赋值判断
    },
    // 修改，注意RESTful风格提交，跨域请求
    async updateBrand () {
      // console.log(this.showBrand)
      await this.$http.put('/goods/brand/updateBrand', this.showBrand)
    },
    handleClose () {
      this.clearSouceData()
    },
    // 提交数据/取消操作进行情况数据的函数封装
    clearSouceData () {
      this.title = 'add'
      this.dialogVisible = false
      // 复制对象结构，清空数据
      this.showBrand = { ...defaultBrandData }
      this.getBrandList()
    },
    // 上传之前的判断
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 上传成功
    successAliyun (response) {
      this.showBrand.image = response.data.imgUrl
    }
  }
}
</script>

<style scoped>
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
